package com.gas;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class PgSqlMybatisDemo {

    private static final Logger logger = LoggerFactory.getLogger(PgSqlMybatisDemo.class);

    static SqlSessionFactory sqlSessionFactory;

    @Before
    public void before() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //SqlSessionFactoryBuilder 这个实例用完就可以抛弃了
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void test() {
        SqlSession sqlSession = null;
        try {
            //  使用hutool工具 读取 rqjg_pipe_section_202510210016.txt 这个文件中的 随机1w行数据 放到 pipeCodes 集合中
            List<String> pipeCodes = FileUtil.readLines(ResourceUtil.getResource("rqjg_pipe_section_202510210016.txt"), Charset.defaultCharset());
            logger.info("pipeCodes size: {}", pipeCodes.size());

            int maxSize = 10000;
            int batchSize = 2000;
            //从 pipeCodes 随机选取5000个元素
            // 使用Stream API随机采样
            List<String> randomPipeCodes = pipeCodes.stream()
                    .collect(Collectors.collectingAndThen(
                            Collectors.toList(),
                            list -> {
                                Collections.shuffle(list);

                                return list.stream().limit(maxSize).collect(Collectors.toList());
                            }
                    ));

            sqlSession = sqlSessionFactory.openSession();
            StopWatch stopWatch = new StopWatch();

            List<List<String>> partition = ListUtil.partition(randomPipeCodes, batchSize);
            for (int i = 0; i < partition.size(); i++) {
                List<String> strings = partition.get(i);
                // 第一个任务
                stopWatch.start(String.format("数据总量%s，分批大小%s，第%s个任务", maxSize,batchSize, i + 1));
                // 把以上修改 改成 获取Mapper 接口。 在执行查询操作
                RqjgGridLayerMapper mapper = sqlSession.getMapper(RqjgGridLayerMapper.class);
                List<RqjgGridLayerPipeCodeExtPo> user = mapper.selectGridLayerGASPT(strings);
                stopWatch.stop();
                logger.info("第{}个任务耗时: {}", i + 1, stopWatch.getLastTaskTimeMillis());
            }
            logger.info(stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
    }

}
